﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class EmailSendDAO
    {
        public EmailSendDAO() { }

        public bool Add(EmailSend EmailSend)
        {
            try
            {
                DBHelper.TransSaveOrUpdate(EmailSend);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Modity(EmailSend EmailSend)
        {
            try
            {
                DBHelper.TransUpdate(EmailSend);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public EmailSend GetProductById(int id)
        {
            return DBHelper.ExecuteHQL("from EmailSend where id = " + id).List<EmailSend>().First(); ;
        }
        public bool Delete(int id)
        {
            try
            {
                DBHelper.TransDeleteByID("EmailSend",id); ;
                return true;
            }
            catch
            {
                return false;
            }
        }
       
        public int GetTotal()
        {
            return Convert.ToInt32(DBHelper.ExecuteHQL("select count(*) from EmailSend").UniqueResult());
        }
        public IList<EmailSend> GetAllEmailSend()
        {
            return DBHelper.ExecuteHQL("from EmailSend  order by addDate desc").List<EmailSend>();
        }
        public IList<EmailSend> GetAllEmailSend(string name)
        {
            return DBHelper.ExecuteHQL("from EmailSend where isUse = true and name like'"+name+"'").List<EmailSend>();
        }

        public EmailSend  GetEmailSendById(int id)
        {
            return (EmailSend)DBHelper.GetObject(typeof(EmailSend), id);
        }
        /// <summary>
        /// 返回false时  可以订阅
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool isExists(string email,string name)
        {
            try
            {
                if (DBHelper.ExecuteHQL("from EmailSend where email like '" + email + "' and name like '"+name+"'").List<EmailSend>().Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch 
            {

                return false;
            }
        }
    }
}
